var NfcServer = {
	
	// Enums
	
	NdefRecordTnf: {
		Empty: "empty",
        WellKnown: "wellKnown",
        Mime: "mime",
        Uri: "uri",
        NfcExternal: "external",
        Unknown: "unknown",
        Unchanged: "unchanged",
        Reserved: "reserved"
	},
	
	NdefRecordChunk: {
		NotChunked: "notChunked",
        ChunkBegin: "chunkBegin",
        ChunkMiddle: "chunkMiddle",
        ChunkEnd: "chunkEnd"
	},
	
	// Properties
	
	get onshow() {
		return _onshow;	
	},
	
	set onshow(callback) {
		NfcServer._settingOnshow = true;
		NfcServer._onshowCallback = callback;
		NfcServer._captureFocusEvents();
		NfcServer._settingOnshow = false;
	},
	
	get onhide() {
		return _onhide;	
	},
	
	set onhide(callback) {
		NfcServer._onhideCallback = callback;
		NfcServer._captureFocusEvents();
	},
	
	// Main Functions
	
 	readTag: function(tnf, type, timeout, onSuccess, onError) {
	 	NfcServer._captureFocusEvents();
	 	
	 	var uri = NfcServer._getBaseUri();
	 	uri = NfcServer._addParam(uri, "method", "readTag");
	 	uri = NfcServer._addParam(uri, "tnf", tnf);
	 	uri = NfcServer._addParam(uri, "type", type);
	 	uri = NfcServer._addParam(uri, "timeout", timeout);
	 	
	 	NfcServer._printDebug(uri);
		NfcServer._sendAPIBridgeRequest(uri, onSuccess, onError);
	},
	
	sendMessage: function(messageId, content, onSuccess, onError) {
	 	var uri = NfcServer._getBaseUri();

	 	uri = NfcServer._addParam(uri, "method", "sendMessage");
	 	uri = NfcServer._addParam(uri, "messageId", messageId);
	 	uri = NfcServer._addParam(uri, "content", content);
	 	
	 	NfcServer._printDebug(uri);
		NfcServer._sendAPIBridgeRequest(uri, onSuccess, onError);
	},
	
	setResponse: function(messageId, content, onSuccess, onError) {
	 	var uri = NfcServer._getBaseUri();

	 	uri = NfcServer._addParam(uri, "method", "setResponse");
	 	uri = NfcServer._addParam(uri, "messageId", messageId);
	 	uri = NfcServer._addParam(uri, "content", content);
	 	
	 	NfcServer._printDebug(uri);
		NfcServer._sendAPIBridgeRequest(uri, onSuccess, onError);
	},

	transactionLog: function(onSuccess, onError) {
	 	var uri = NfcServer._getBaseUri();

	 	uri = NfcServer._addParam(uri, "method", "transactionLog");
	 	
	 	NfcServer._printDebug(uri);
		NfcServer._sendAPIBridgeRequest(uri, onSuccess, onError);
	},

	// Private stuff
	
	_widgetPath: null,
	_onshowCallback: null,
	_onhideCallback: null,
	_settingOnshow: false,
	_capturingFocusEvents: false,
	
	_getBaseUri: function() {
		if(NfcServer._widgetPath == null) {
			NfcServer._widgetPath = APIBridge.Internal._getWidgetPath();
			var re = new RegExp(".*/(.*/.*)");
			var match = re.exec(NfcServer._widgetPath);
			if(match) {
				NfcServer._widgetPath = encodeURIComponent(match[1]);
			} else {
				NfcServer._widgetPath = encodeURIComponent(NfcServer._widgetPath);
			}
		}
		uri = "/nfc?widgetId=" + NfcServer._widgetPath;
		return uri;
	},
	
	_addParam: function(uri, param, value) {
		uri += (uri.indexOf("?") == -1) ? "?" : "&";
		uri += param;
		uri += "=";
		uri += value;
		return uri;
	},
	
	_printDebug: function(msg) {
		var debug_output = document.getElementById('nfc_api_debug');
		if (debug_output)
		{
			debug_output.innerHTML += msg + "<br />";
		}
	},
	
	_sendAPIBridgeRequest: function(uri, onSuccess, onError) {
		APIBridge.Internal._sendRequest( 
			uri, 
			null,
			function(req) {
				if (req.responseText)
					onSuccess(req.responseText);
				else
					onError(500);					
			},
			onError );
	},
	
	_setForeground: function(onForeground) {
			var uri = NfcServer._getBaseUri();
	 	uri = NfcServer._addParam(uri, "method", "setForeground");
	 	uri = NfcServer._addParam(uri, "onForeground", onForeground);
	 	
	 	//NfcServer._printDebug(uri);
		NfcServer._sendAPIBridgeRequest(uri, NfcServer._internalOnSuccess, NfcServer._internalOnError);
	},
	
	_captureFocusEvents: function() {
		NfcServer._capturingFocusEvents = true;
		widget.onhide = NfcServer._onHide;
		widget.onshow = NfcServer._onShow;
		NfcServer._capturingFocusEvents = false;
	},
	
	_onHide: function() {
		//NfcServer._printDebug("OnHide()");
		NfcServer._setForeground("false");
		
		if(NfcServer._onhideCallback && !NfcServer._capturingFocusEvents) {
			NfcServer._onhideCallback();
		}
	},
	
	_onShow: function() {
		//NfcServer._printDebug("OnShow()");
		NfcServer._setForeground("true");
		
		if(NfcServer._onshowCallback &&
				(!NfcServer._capturingFocusEvents || NfcServer._settingOnshow)) {
			NfcServer._onshowCallback();
		}
	},
	
	_internalOnError: function(errorCode) {
		NfcServer._printDebug("NFC internal error " + errorCode);
	},
	
	_internalOnSuccess: function(response) {
		// Do nothing...
	}
}
